package com.panfeng.xcloud.boss.provider.member.web.rpc;

import com.alibaba.fastjson.JSON;
import com.panfeng.xcloud.boss.provider.dto.request.SendEmailReqDTO;
import com.panfeng.xcloud.boss.provider.member.dao.mapper.SendEmailLogMapperExt;
import com.panfeng.xcloud.boss.provider.member.dto.request.EmailCodeReqDTO;
import com.panfeng.xcloud.boss.provider.member.service.IUserSMSService;
import com.panfeng.xcloud.boss.provider.rpc.member.UserInfoFeignApi;
import com.panfeng.xcloud.common.core.configuration.DataDictionaryConfig;
import com.panfeng.xcloud.common.core.enums.DataDictionaryTypeEnum;
import com.panfeng.xcloud.common.core.enums.VerifiyCodeTypeEnum;
import com.panfeng.xcloud.common.core.web.vo.ResponseVO;
import com.panfeng.xcloud.common.security.annotation.NotNeedAccess;
import com.panfeng.xcloud.common.security.annotation.NotNeedFeignAccess;
import com.panfeng.xcloud.common.security.web.support.BaseController;
import com.panfeng.xcloud.dao.member.entity.SendEmailLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@Api(value = "API - UserInfoApiFeignClient", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UserInfoApiFeignClient extends BaseController implements UserInfoFeignApi {

    @Autowired
    private IUserSMSService iUserSMSService;

    @Autowired
    private DataDictionaryConfig dataDictionaryConfig;

    @Autowired
    private SendEmailLogMapperExt sendEmailLogMapperExt;

    @NotNeedAccess
    @NotNeedFeignAccess
    @PostMapping(value = "/feignApi/member/sendEmail", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(httpMethod = "POST", value = "rest-rpc处理发送邮件操作")
    @Override
    public ResponseVO sendEmail(@RequestBody SendEmailReqDTO sendEmailReqDTO) {
        log.info(">>> sendEmail参数:{} <<<<", (null != sendEmailReqDTO ? JSON.toJSONString(sendEmailReqDTO) : ""));

        String email = dataDictionaryConfig.getCodeToNameMap().get(DataDictionaryTypeEnum.SEND_ERROR_EMAIL.getCode());

        EmailCodeReqDTO emailCodeReqDTO = new EmailCodeReqDTO();
        emailCodeReqDTO.setEmail(email);
        emailCodeReqDTO.setVerifiyCodeTypeEnum(VerifiyCodeTypeEnum.ACCOUNT_SECURITY);

        //boolean result = iUserSMSService.sendEmail(emailCodeReqDTO, sendEmailReqDTO);
        SendEmailLog sendEmailLog = new SendEmailLog();
        sendEmailLog.setNote(sendEmailReqDTO.getExceptionMessage());
        int i = sendEmailLogMapperExt.insertSelective(sendEmailLog);
        boolean result = i > 0 ? true : false;

        if (result) {
            return ResponseVO.success();
        } else {
            return ResponseVO.error();
        }
    }

}
